package com.rfid.project.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.data.domain.Page;
import org.codehaus.jackson.map.ObjectMapper;

import biz.core.support.spring.DateEditor;
import biz.appframe.BizObjectMapper;
import biz.appframe.PageJsonBean;

import com.rfid.project.exception.CannotDelException;
import com.rfid.project.model.TechnicalTitle;
import com.rfid.project.service.TechnicalTitleService;
import com.rfid.project.util.StringUtil;

@Controller
public class TechnicalTitleController {
	
	private static final Logger logger = LoggerFactory.getLogger(TechnicalTitleController.class);
	
	@Autowired
	private TechnicalTitleService technicalTitleService;
	
	@RequestMapping(value = "/technicalTitlemgr")
	public String technicalTitlemgr(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		return "technicalTitlemgr";
	}
	
	@RequestMapping(value = "/queryTechnicalTitle")
	public String queryTechnicalTitle(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "name", defaultValue = "") String name,
			@RequestParam(value = "workTimeFactor", defaultValue = "") Double workTimeFactor,
			@RequestParam(value = "remark", defaultValue = "") String remark,
			@RequestParam(value = "page", defaultValue = "1") Integer pageNumber,
			@RequestParam(value = "rows", defaultValue = "10") Integer pageSize,
			@RequestParam(value = "sort", defaultValue = "") String sort,
			@RequestParam(value = "order", defaultValue = "") String order,
			Model model) {
		Page<TechnicalTitle> technicalTitles = technicalTitleService.findAll(name,workTimeFactor,remark, pageNumber, pageSize, sort, order);
		JSONObject object = new JSONObject();
		object.put("total", technicalTitles.getTotalElements());
		JSONArray models = new JSONArray();
		for (TechnicalTitle technicalTitle : technicalTitles) {
			JSONObject mod = new JSONObject();
			wrapData(technicalTitle, mod);

			models.add(mod);
		}
		object.put("rows", models);
		PageJsonBean.printObject(response, object);
		return null;
	}
	
	@RequestMapping(value = "/findTechnicalTitle")
	public String findTechnicalTitle(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "") Long id,
			Model model) {
		TechnicalTitle technicalTitle = technicalTitleService.findOne(id);
		JSONObject object = new JSONObject();
		JSONObject mod = new JSONObject();
		wrapData(technicalTitle, mod);
		object.put("success", true);
		object.put("data", mod);
		PageJsonBean.printObject(response, object);
		return null;
	}

	private void wrapData(TechnicalTitle technicalTitle, JSONObject mod) {
		mod.put("id", technicalTitle.getId());
		mod.put("name", technicalTitle.getName());
		mod.put("orderNumber", technicalTitle.getOrderNumber());
		mod.put("workTimeFactor", technicalTitle.getWorkTimeFactor());
		mod.put("remark", StringUtil.getString(technicalTitle.getRemark()));
	}
	@RequestMapping(value = "/editTechnicalTitle", method = RequestMethod.POST)
	public String editTechnicalTitle(HttpServletRequest request,
			HttpServletResponse response, @Valid TechnicalTitle technicalTitle,
			Model model, RedirectAttributes redirectAttributes) {
		technicalTitleService.saveTechnicalTitle(technicalTitle);

		JSONObject result = new JSONObject();
		result.put("success", true);
		PageJsonBean.printObject(response, result);
		return null;
	}

	@RequestMapping(value = "/delTechnicalTitle")
	public String delTechnicalTitle(
			HttpServletRequest request, 
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "") Long id) {
		JSONObject result = new JSONObject();
		boolean		flag = true;
		try {
			this.technicalTitleService.deleteTechnicalTitle(id);
		} catch (CannotDelException e) {
			flag = false;
			result.put("msg",e.getMessage());
		}
		result.put("success", flag);
		PageJsonBean.printObject(response, result);
		return null;
	}
	
	@RequestMapping(value = "/technicalTitleChangeOrderNumber")
	public String changeOrderNumber(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "") Long id,
			@RequestParam(value = "targetId", defaultValue = "") Long targetId){
		JSONObject result = new JSONObject();
		boolean flag = this.technicalTitleService.changeOrderNumber(id, targetId);
		result.put("success", flag);
		PageJsonBean.printObject(response, result);
		return null;
	}
}

